package com.mingqijia.gassafety.authority.web.facade.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.mingqijia.gassafety.authority.api.web.facade.CommonManageFacade;
import com.mingqijia.gassafety.authority.domain.MessageProvider;
import com.mingqijia.gassafety.shared.facade.BaseFacade;
import com.mingqijia.gassafety.shared.handler.GlobalAuthenticationException;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 授权管理
 * 
 * @author Damon S.
 * @version v0.2
 * @date {YEAR}年{MONTH}月{DAY}日 {TIME}
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CommonManageFacadeImpl extends BaseFacade implements CommonManageFacade {

    /** */
    private static Map<String, String> contentTypeMap = new HashMap<>();

    private final MessageProvider mProvider;

    static {
        contentTypeMap.put(".3gp", "video/3gpp");
        contentTypeMap.put(".amc", "application/x-mpeg");
        contentTypeMap.put(".apk", "application/vnd.android.package-archive");
        contentTypeMap.put(".avi", "video/x-msvideo");
        contentTypeMap.put(".awb", "audio/amr-wb");
        contentTypeMap.put(".bas", "text/plain");
        contentTypeMap.put(".bmp", "image/bmp");
        contentTypeMap.put(".csv", "text/plain");
        contentTypeMap.put(".doc", "application/msword");
        contentTypeMap.put(".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        contentTypeMap.put(".dwf", "drawing/x-dwf");
        contentTypeMap.put(".dwt", "application/x-autocad");
        contentTypeMap.put(".dws", "application/x-autocad");
        contentTypeMap.put(".dwg", "application/x-autocad");
        contentTypeMap.put(".dxf", "application/x-autocad");
        contentTypeMap.put(".fpx", "image/x-fpx");
        contentTypeMap.put(".fvi", "video/isivideo");
        contentTypeMap.put(".gif", "image/gif");
        contentTypeMap.put(".gz", "application/x-gzip");
        contentTypeMap.put(".hdf", "application/x-hdf");
        contentTypeMap.put(".jpe", "image/jpeg");
        contentTypeMap.put(".jpeg", "image/jpeg");
        contentTypeMap.put(".jpg", "image/jpeg");
        contentTypeMap.put(".jpz", "image/jpeg");
        contentTypeMap.put(".log", "text/plain");
        contentTypeMap.put(".m4a", "audio/mp4a-latm");
        contentTypeMap.put(".m4b", "audio/mp4a-latm");
        contentTypeMap.put(".m4p", "audio/mp4a-latm");
        contentTypeMap.put(".m4u", "video/vnd.mpegurl");
        contentTypeMap.put(".m4v", "video/x-m4v");
        contentTypeMap.put(".ma1", "audio/ma1");
        contentTypeMap.put(".ma2", "audio/ma2");
        contentTypeMap.put(".ma3", "audio/ma3");
        contentTypeMap.put(".ma5", "audio/ma5");
        contentTypeMap.put(".mid", "audio/mid");
        contentTypeMap.put(".midi", "audio/midi");
        contentTypeMap.put(".tar.gz", "application/x-zip-compressed");
        contentTypeMap.put(".mil", "image/x-cals");
        contentTypeMap.put(".mio", "audio/x-mio");
        contentTypeMap.put(".mng", "video/x-mng");
        contentTypeMap.put(".mov", "video/quicktime");
        contentTypeMap.put(".movie", "video/x-sgi-movie");
        contentTypeMap.put(".mp2", "video/mpeg");
        contentTypeMap.put(".mp3", "audio/mpeg");
        contentTypeMap.put(".mp4", "video/mp4");
        contentTypeMap.put(".mpa", "video/mpeg");
        contentTypeMap.put(".mpe", "video/mpeg");
        contentTypeMap.put(".mpeg", "video/mpeg");
        contentTypeMap.put(".mpg", "video/mpeg");
        contentTypeMap.put(".mpg4", "video/mp4");
        contentTypeMap.put(".mpga", "audio/mpeg");
        contentTypeMap.put(".mpv2", "video/mpeg");
        contentTypeMap.put(".mrl", "text/x-mrml");
        contentTypeMap.put(".nbmp", "image/nbmp");
        contentTypeMap.put(".pac", "audio/x-pac");
        contentTypeMap.put(".pae", "audio/x-epac");
        contentTypeMap.put(".pbm", "image/x-portable-bitmap");
        contentTypeMap.put(".pcx", "image/x-pcx");
        contentTypeMap.put(".pda", "image/x-pda");
        contentTypeMap.put(".pdf", "application/pdf");
        contentTypeMap.put(".pfr", "application/font-tdpfr");
        contentTypeMap.put(".pfx", "application/x-pkcs12");
        contentTypeMap.put(".pgm", "image/x-portable-graymap");
        contentTypeMap.put(".pict", "image/x-pict");
        contentTypeMap.put(".png", "image/png");
        contentTypeMap.put(".pnm", "image/x-portable-anymap");
        contentTypeMap.put(".pnz", "image/png");
        contentTypeMap.put(".pot,", "application/vnd.ms-powerpoint");
        contentTypeMap.put(".ppm", "image/x-portable-pixmap");
        contentTypeMap.put(".pps", "application/vnd.ms-powerpoint");
        contentTypeMap.put(".ppt", "application/vnd.ms-powerpoint");
        contentTypeMap.put(".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        contentTypeMap.put(".pqf", "application/x-cprplayer");
        contentTypeMap.put(".pqi", "application/cprplayer");
        contentTypeMap.put(".prc", "application/x-prc");
        contentTypeMap.put(".prf", "application/pics-rules");
        contentTypeMap.put(".rgb", "image/x-rgb");
        contentTypeMap.put(".rmf", "audio/x-rmf");
        contentTypeMap.put(".rmi", "audio/mid");
        contentTypeMap.put(".rmm", "audio/x-pn-realaudio");
        contentTypeMap.put(".rmvb", "audio/x-pn-realaudio");
        contentTypeMap.put(".rp", "image/vnd.rn-realpix");
        contentTypeMap.put(".rpm", "audio/x-pn-realaudio-plugin");
        contentTypeMap.put(".svg", "image/svg+xml");
        contentTypeMap.put(".svf", "image/vnd");
        contentTypeMap.put(".svh", "image/svh");
        contentTypeMap.put(".svr", "x-world/x-svr");
        contentTypeMap.put(".swf", "application/x-shockwave-flash");
        contentTypeMap.put(".swfl", "application/x-shockwave-flash");
        contentTypeMap.put(".tar", "application/x-tar");
        contentTypeMap.put(".taz", "application/x-tar");
        contentTypeMap.put(".txt", "text/plain");
        contentTypeMap.put(".wav", "audio/webm");
        contentTypeMap.put(".xls", "application/vnd.ms-excel");
        contentTypeMap.put(".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        contentTypeMap.put(".xml", "text/plain");
        contentTypeMap.put(".zip", "application/zip");
        contentTypeMap.put(".json", "application/json");
    }

    @Override
    public String uploadFile(MultipartFile file) {
        checkFileType(file);
        try {
            return mProvider.uploadStorage(file);
        }
        catch (Exception e) {
            log.error("上传文件失败【fileName={}】", file.getOriginalFilename(), e);
            throw new GlobalAuthenticationException("上传文件失败");
        }
    }

    @Override
    public String uploadImage(MultipartFile file) {
        checkFileType(file);

        try {
            return mProvider.uploadImage(file);
        }
        catch (Exception e) {
            log.error("上传图片失败【fileName={}】", file.getOriginalFilename(), e);
            throw new GlobalAuthenticationException("上传图片失败");
        }
    }

    @Override
    public void downloadFile(String fileName, HttpServletResponse response) {
        try {
            mProvider.rebuildStorage(response, fileName);
        }
        catch (Exception e) {
            log.error("获取文件失败【fileName={}】", fileName, e);
            throw new GlobalAuthenticationException("加载文件失败");
        }
    }

    private void checkFileType(final MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String fileExtName = "";
        if (StringUtils.isNotEmpty(fileName) && fileName.indexOf(".") != -1) {
            fileExtName = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        }
        if (!contentTypeMap.containsKey(fileExtName.toLowerCase())
            || !contentTypeMap.containsValue(file.getContentType())) {
            throw new GlobalAuthenticationException("文件类型不支持");
        }
    }
}
